export const numbers = {
  zoomIn(baseValue, Precision) {
    let arr = (baseValue + "").split(".");
    let intStr = arr[0],
      floatStr = arr[1];
    if (floatStr) {
      //小数部分
      floatStr = floatStr.padEnd(Precision, 0);
      return +(intStr + floatStr);
    } else {
      intStr = intStr.padEnd(intStr.length + Precision, 0);
      return +intStr;
    }
  },
  getPrecision(params) {
    let precision = 0;
    for (let i = 0, len = params.length; i < len; i++) {
      let item = params[i] + "";
      let arr = item.match(/\.([\d]+)/);
      if (arr) {
        if (precision < arr[1].length) {
          precision = arr[1].length;
        }
      }
    }
    if (precision) {
      precision += 1;
    }
    return precision;
  },
  /**
   * 加法
   * @returns
   */
  sum() {
    let params = [...arguments];
    //undefined处理
    params = params.map(item => {
      if (item === undefined) {
        return 0;
      } else {
        return item;
      }
    });
    let factor = Math.pow(10, this.getPrecision(params)),
      precision = this.getPrecision(params),
      ret = 0;
    for (let i = 0, len = params.length; i < len; i++) {
      // ret += (+params[i])*factor
      ret += this.zoomIn(params[i], precision);
    }
    ret = ret / factor;
    //console.log("sum ret is ", params, ret)
    return ret;
  },
  /**
   * 减法
   * @returns
   */
  sub() {
    let params = [...arguments],
      factor = Math.pow(10, this.getPrecision(params)),
      precision = this.getPrecision(params),
      // ret = params[0]*factor
      ret = this.zoomIn(params[0], precision);
    for (let i = 1, len = params.length; i < len; i++) {
      // ret -= (+params[i])*factor
      ret -= this.zoomIn(+params[i], precision);
    }
    ret = +ret / factor;
    //console.log("   sub ret is ", params, ret)
    return ret;
  },
  /**
   * 乘法
   * @returns
   */
  multiplication() {
    let params = [...arguments],
      factor = Math.pow(10, this.getPrecision(params)),
      precision = this.getPrecision(params),
      ret = params[0] * factor;
    for (let i = 1, len = params.length; i < len; i++) {
      // ret *= (+params[i])*factor
      ret *= this.zoomIn(+params[i], precision);
    }
    ret = +ret / Math.pow(factor, params.length);
    //console.log("multiplication ret is ", params, ret)
    return ret;
  },
  /**
   * 除法
   * @returns
   */
  division() {
    let params = [...arguments];
    let ret = params[0];
    for (let i = 1, len = params.length; i < len; i++) {
      ret = ret / params[i];
    }
    return ret;
  },
};
